package http;

import java.util.HashMap;
import java.util.Map;

/**
 * 模拟http请求响应报文
 */
public class Response {

    public final static String HTTP1_1 = "HTTP/1.1";

    /**
     * 常见http响应状态码
     */
    public final static int OK = 200; //请求成功
    public final static int Bad_Request = 400; //客户端请求有语法错误，不能被服务器所理解。
    public final static int Unauthorized = 401; //请求未经授权，这个状态代码必须和WWW-Authenticate报头域一起使用。
    public final static int Forbidden = 403; //服务器收到请求，但是拒绝提供服务。
    public final static int Not_Found = 404; //请求资源不存在
    public final static int Internal_Server_Error = 500; //服务器发生不可预期的错误。
    public final static int Server_Unavailable = 503; //服务器当前不能处理客户端的请求，一段时间后可能恢复正常

    /**
     * 状态码对应的状态描述
     */
    public final static Map<Integer, String> statusDescriptionMap = new HashMap<Integer, String>() {
        {
            put(200, "OK");
            put(400, "Bad Request");
            put(401, "Unauthorized");
            put(403, "Forbidden");
            put(404, "Not_Found");
            put(500, "Internal Server Error");
            put(503, "Server Unavailable");
        }
    };

    /**
     * 常见响应头字段名称
     */
    public final static String Content_Type = "Content-Type"; //响应体的数据类型
    public final static String Content_Length = "Content-Length"; //响应体的数据长度

    /**
     * 常见响应体类型
     */
    public final static String Json_Content_Type = "application/json"; //json类型
    public final static String Plain_Content_Type = "text/plain"; //文本类型
    public final static String HTML_Content_Type = "text/html"; //html类型

    public Response() {}

    public Response(int status_code) {
        this.setVersion(HTTP1_1);
        this.setStatus_code(status_code);
        this.setStatus_description(statusDescriptionMap.get(status_code));
    }

    /**
     * http版本
     */
    private String version;

    /**
     * 状态码
     */
    private int status_code;

    /**
     * 状态描述
     */
    private String status_description;

    /**
     * 响应头
     */
    private Map<String, String> headers;

    /**
     * 响应体
     */
    private String body;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public int getStatus_code() {
        return status_code;
    }

    public void setStatus_code(int status_code) {
        this.status_code = status_code;
    }

    public String getStatus_description() {
        return status_description;
    }

    public void setStatus_description(String status_description) {
        this.status_description = status_description;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

}
